package com.franklin.java.api.psi.data.resolver;

import com.franklin.java.api.psi.data.PsiData;
import com.franklin.java.utils.PsiUtils;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.util.PsiTreeUtil;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Franklin
 * @since 2021/08/15/ 21:25
 */
public class MethodParameterResolver implements PsiDataResolver {
    @Override
    public PsiData resolve(String name, String value, PsiElement dataElement) {
        PsiMethod psiMethod = PsiTreeUtil.getParentOfType(dataElement, PsiMethod.class);
        if (Objects.nonNull(psiMethod)){
            //从参数列表里面找
            PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
            if (ArrayUtils.isNotEmpty(psiParameters)){
                List<PsiParameter> psiParameterList = Arrays.stream(psiParameters)
                        .filter(psiParameter -> psiParameter.getName().equals(name))
                        .collect(Collectors.toList());
                PsiData psiData = resolveVariable(dataElement, value, psiParameterList);
                if (Objects.nonNull(psiData)){
                    return psiData;
                }
            }
        }
        return null;
    }
}
